//
// File   : State.cs
// Author : Dylan Gleason
// Class  : CST229 - Grammars
// 
// State class represents a single State in the 
// a FiniteAutomaton, including its transitions
//


namespace CST229_Assignment2
{
   public class State
   {
      //
      // Private member variables for State
      //
      private char m_name;
      private bool m_isAccepted;
      private State m_transitionA;
      private State m_transitionB;

      public State (char name)
      {
         m_name = name;
         m_isAccepted = false;
      }

      //
      // Property lists for all of the State objects fields
      //
      public char Name
      {
         get { return m_name; }
         set { m_name = value; }
      }

      public bool IsAccepted
      {
         get { return m_isAccepted; }
         set { m_isAccepted = value; }
      }

      public State TransitionA 
      {
         get { return m_transitionA; }
         set { m_transitionA = value; }
      }

      public State TransitionB
      {
         get { return m_transitionA; }
         set { m_transitionB = value; }
      }

      //
      // Test method will test to see if a given expression is valid
      // or not. It does so by comparing each character in the string
      // via successive recursive calls on its A and B transitions.
      //
      public bool Test(string expr)
      {
         if (expr == string.Empty)
            return m_isAccepted;
         else if (expr[0] =='a')
            return m_transitionA.Test (expr.Substring (1, expr.Length - 1));
         else if (expr[0] == 'b')
            return m_transitionB.Test (expr.Substring (1, expr.Length - 1));
         else 
            return false;
      }
   }
}
